import spyral
import pygame

import Dic;
import Button;
import Story;
import Menu;

class GameOver(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.camera = spyral.director.get_camera()
        self.group = spyral.sprite.Group(self.camera)
        self.player = player;
        
        title = spyral.sprite.Sprite()
        title.image = Dic.fonts['menu_title'].render(
                            "Gameover",
                            True,
                            Dic.colors['menu'])
        title.rect.center = self.camera.get_rect().center
        
        score = spyral.sprite.Sprite()
        score.image = Dic.fonts['menu'].render(
                            "GAMES COMPLETED: " + repr(self.player.getCurrentGamesCompleted()),
                            True,
                            Dic.colors['menu'])
        score.rect.midtop = (title.rect.center[0], title.rect.bottom + 5)

        self.retry = Button.Button(Dic.images['retry'], (150, 480));
        self.menu = Button.Button(Dic.images['menu'], (500, 480))

        self.group.add(title, score, self.retry, self.menu)
    
    def on_enter(self):
        bg = spyral.util.new_surface((960,640))
        bg.fill(Dic.colors['bg'])
        self.camera.set_background(bg)

          
    def render(self):
        self.group.draw()
        self.camera.draw()
        
    def update(self, tick):
        #input handler
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
            if event.type == pygame.MOUSEBUTTONDOWN:
                if (self.retry.rect.collidepoint(pygame.mouse.get_pos()) == True):
                    spyral.director.replace(Story.Story(self.player))
                elif (self.menu.rect.collidepoint(pygame.mouse.get_pos()) == True):
                    spyral.director.pop();